using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using TidhiGanitam.DataModel;

namespace TidhiGanitam.Ganitam
{
    public class Panchanzyamu
    {
        int m_Year;
        DateTime m_Ugadi;
        ArrayList m_RaviRoutines;
        ArrayList m_ChandraRoutines;
        ArrayList m_DinamuRoutines;
        ArrayList m_DailyRoutines;
        YearDetails m_CurrentYearDetails;
        public ArrayList DailyCalculations
        {
            get
            { return m_DailyRoutines; }
            set
            { m_DailyRoutines = value; }
        }
        public Panchanzyamu(int Year, YearDetails CurrentYearDetails, ArrayList RaviRoutines, ArrayList ChandraRoutines,
            ArrayList DinamuRoutines)
        {
            m_Year = Year;
            m_Ugadi = CurrentYearDetails.UgadiDate;
            m_RaviRoutines = RaviRoutines;
            m_ChandraRoutines = ChandraRoutines;
            m_DinamuRoutines = DinamuRoutines;
            m_CurrentYearDetails = new YearDetails(CurrentYearDetails);
        }
        public void DoDailyRoutinesForYear()
        {
            m_DailyRoutines = new ArrayList();
            PanchanzyamuRoutineItem PreviousPanchanzyamuItem = SetFirstPanchanzyamuRoutineItem();
            double Direction = 1;
            //here we run the loop only for 399 times because 
            //when we calculate tidhulu, we check for eshyam which will go to the next day.
            //so when running for 399th day, it goes for 400th day.
            for (int iSNo = 0; iSNo < 399; iSNo++)
            {
                PanchanzyamuRoutineItem RoutineItem = new PanchanzyamuRoutineItem(iSNo, m_Year, m_Ugadi.AddDays(iSNo),
                    m_RaviRoutines, m_ChandraRoutines, m_DinamuRoutines, m_CurrentYearDetails);
                RoutineItem.Calculate(PreviousPanchanzyamuItem, ref Direction);
                m_DailyRoutines.Add(RoutineItem);
                PreviousPanchanzyamuItem = RoutineItem;
            }
        }
        private PanchanzyamuRoutineItem SetFirstPanchanzyamuRoutineItem()
        {
            PanchanzyamuRoutineItem FirstRoutine = new PanchanzyamuRoutineItem(0, m_Year, m_Ugadi,
                m_RaviRoutines, m_ChandraRoutines, m_DinamuRoutines, m_CurrentYearDetails);

            double CurrentRunningTidhi = -1;
            int CurrentRunningMasamu = 11;
            Enums.TidhiState PreviousTidhiState = Enums.TidhiState.Ending;
            
            ChandraRoutineItem FirstChandra = (ChandraRoutineItem)m_ChandraRoutines[0];
            RaviRoutineItem FirstRavi = (RaviRoutineItem)m_RaviRoutines[0];
            DinamuRoutineItem FirstDinamu = (DinamuRoutineItem) m_DinamuRoutines[0];

            TidhiRoutineItem FirstTidhi = new TidhiRoutineItem(0, m_Year, m_Ugadi, CurrentRunningTidhi,
                CurrentRunningMasamu, PreviousTidhiState, m_RaviRoutines, m_ChandraRoutines, m_DinamuRoutines, m_CurrentYearDetails);
            YogamuRoutineItem FirstYogamu = new YogamuRoutineItem(0, m_Year, m_Ugadi, 0, Enums.TidhiState.Ending,
                m_RaviRoutines, m_ChandraRoutines, m_DinamuRoutines);
            NakshatramuRoutineItem FirstNakshatramu = new NakshatramuRoutineItem(0, m_Year, m_Ugadi, 0, Enums.TidhiState.Ending,
                m_RaviRoutines, m_ChandraRoutines, m_DinamuRoutines);
            VarjyamuRoutineItem FirstVarjyamu = new VarjyamuRoutineItem(0, m_Year, m_Ugadi, FirstNakshatramu, FirstDinamu);
            AmrutaGhadiyaluRoutineItem FirstAmruGhadiyalu = new AmrutaGhadiyaluRoutineItem(0, m_Year, m_Ugadi,
                FirstNakshatramu, FirstDinamu);
            ArkahaRoutineItem FirstArkaha = new ArkahaRoutineItem(0, m_Year, m_Ugadi, FirstRavi, FirstDinamu);
            BhuktiRoutineItem FirstBhukti = new BhuktiRoutineItem(0, m_Year, m_Ugadi, FirstDinamu, FirstRavi);

            //Tidhi
            FirstRoutine.TidhiRoutine = FirstTidhi;
            //Nakshatramu
            FirstRoutine.NakshatramuRoutine = FirstNakshatramu;
            //Karanamu
            FirstRoutine.KaranamuRoutine = new KaranamuRoutineItem(FirstTidhi, CurrentRunningTidhi);
            //Yogamu            
            FirstRoutine.YogamuRoutine = FirstYogamu;
            //Varjyamu
            FirstRoutine.VarjyamuRoutine = FirstVarjyamu;
            //Amruta Ghadiyalu
            FirstRoutine.AmrutaGhadiyalu = FirstAmruGhadiyalu;
            //Durmuhurtaalu
            FirstRoutine.Durmuhurtaalu = new DurmuhurtamuRoutineItem(0, m_Year, m_Ugadi, FirstDinamu);
            //Arkaha
            FirstRoutine.Arkaha = FirstArkaha;
            //Bhukti
            FirstRoutine.Bhukti = FirstBhukti;

            return FirstRoutine;
        }

    }
}
